//
//	This is an example of how to use the cool scrollbar library.
//	This example does alot, because it shows how to use all
//	the features of the library (custom draw, inserted buttons, tooltips etc)
//
//	History:
//		3/5/2001. Updated to fix focus problem.
//
#pragma warning( disable : 4115)

#include <windows.h>
#include <commctrl.h>

#include "trace.h"

#include "resource.h"
#include "aggressiveoptimize.h"
#include "coolscroll.h"
#include "gripper.h"
#include "test.h"

#define VERT_GRIPPER 700
#define GRIPPER 1
#define BUT1	2
#define BUT2	3

//	Tooltip lookup
//
struct
{
	UINT cmd;
	char text[32];
} TipLookup[] = 
{
	0,   "Gripper",
	650, "Left arrow",
	651, "Right arrow",
	666, "owner drawn",
	668, "gipper",
	VERT_GRIPPER, "but 1",
	681, "but 2",
	680, "but 3"
};

HINSTANCE	hInst;
HMENU		hmenu;
HDC			hdcSkin;
HBITMAP		hSkinBmp;

BOOL fCustomDraw = FALSE;
BOOL fButtons = TRUE;
BOOL fThumbAlways = FALSE;

HWND hwndScroll1 = 0;
HWND hwndScroll2 = 0;

void MakeCoolEdit(HWND hwndEdit);

//
// Scrollbar position data
//
int nHPageMax = 100;
int nHScrollPos = 0;
int nHScrollPageSize = 9;
int nVPageMax = 100;
int nVScrollMax;
int nVScrollPos = 0;
int nVScrollPageSize = 9;
int nPageMaxLines;
int nNumLines = 0;
int xChar = 10, yChar = 13;

char *BigFile = 0;		//pointer to the text that we will draw
int BigFileLen = 0;		

HENHMETAFILE hemf;
HPEN hpen, oldpen;
HPEN whitepen;
HFONT hfont;

void SetupScrollbars(HWND hwnd)
{
	SCROLLINFO sInfo;
	RECT rect;

	GetClientRect(hwnd, &rect);
	
	nPageMaxLines = min(nNumLines   + 1, rect.bottom / yChar);
	nVScrollMax = max(0, nNumLines-1   + 1);
    nVScrollPos = min(nVScrollPos, nVScrollMax-nPageMaxLines+1);
	
	sInfo.cbSize = sizeof(SCROLLINFO);
	sInfo.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_DISABLENOSCROLL;
	sInfo.nMin	= 0;
	sInfo.nMax	= nVScrollMax;
	sInfo.nPos	= nVScrollPos;
	sInfo.nPage	= min(nPageMaxLines, nVScrollMax+1);

	CoolSB_SetScrollInfo (hwnd, SB_VERT, &sInfo, TRUE);
}

void OffsetPoints(POINT *pts, int numpoints, int dx, int dy)
{
	int i;
	for(i = 0; i < numpoints; i++)
	{
		pts[i].x += dx;
		pts[i].y += dy;
	}
}

void RedrawNC(HWND hwnd)
{
	SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE|
		SWP_FRAMECHANGED|SWP_DRAWFRAME);

}
void ToggleMenuCheck(HMENU menu, UINT menuid)
{
	MENUITEMINFO mii;

	mii.cbSize = sizeof(mii);
	mii.fMask =MIIM_STATE;
	GetMenuItemInfo(hmenu, menuid, FALSE, &mii);

	if(mii.fState & MFS_CHECKED)
		CheckMenuItem(menu, menuid, MF_UNCHECKED);
	else
		CheckMenuItem(menu, menuid, MF_CHECKED);
}


void InsertButtons(HWND hwnd)
{
	SCROLLBUT sbut;
	//
	//	Horizontal buttons
	//
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT | SBBF_CURSOR;
	sbut.uCmdId		= 0;
	sbut.nSize		= 7;
	sbut.uPlacement = SBBP_LEFT;
	sbut.uButType   = SBBT_FIXED;
	sbut.hCurs		= LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZELR));
	
	CoolSB_InsertButton(hwnd, SB_HORZ, (UINT)-1, &sbut);
	
	
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT;
	sbut.uCmdId		= 650;
	sbut.nSize		= -1;
	sbut.uPlacement = SBBP_LEFT;
	sbut.uButType   = SBBT_PUSHBUTTON | SBBM_LEFTARROW;
	CoolSB_InsertButton(hwnd, SB_HORZ, (UINT)-1, &sbut);
	
	
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT;
	sbut.uCmdId		= 651;
	sbut.nSize		= -1;
	sbut.uPlacement = SBBP_LEFT;
	sbut.uButType   = SBBT_PUSHBUTTON | SBBM_RIGHTARROW;
	CoolSB_InsertButton(hwnd, SB_HORZ, (UINT)-1, &sbut);
	
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT ;
	sbut.uCmdId		= 666;
	sbut.nSize		= 120;
	sbut.uPlacement = SBBP_LEFT;
	sbut.uButType   = SBBT_OWNERDRAW | SBBM_RESIZABLE;//SBBT_PUSHBUTTON2;
	sbut.nMinSize	= 0;
	sbut.nMaxSize	= (UINT)-1;
	
	CoolSB_InsertButton(hwnd, SB_HORZ, (UINT)-1, &sbut);
	
	
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT | SBBF_CURSOR;
	sbut.uCmdId		= 668;
	sbut.nSize		= 5;
	sbut.uPlacement = SBBP_LEFT;
	sbut.uButType   = SBBT_FIXED;
	sbut.hCurs		= LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZELR));
	CoolSB_InsertButton(hwnd, SB_HORZ, (UINT)-1, &sbut);
	
	//
	//	Vertical buttons
	//
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT | SBBF_BITMAP;
	sbut.uCmdId		= 680;
	sbut.nSize		= -1;
	sbut.uPlacement = SBBP_RIGHT;
	sbut.uButType   = SBBT_PUSHBUTTON | SBBM_TYPE2 | SBBM_RECESSED;
	sbut.hBmp		= LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
	CoolSB_InsertButton(hwnd, SB_VERT, (UINT)-1, &sbut);
	
	sbut.uCmdId		= 681;
	sbut.nSize		= -1;
	sbut.uPlacement = SBBP_RIGHT;
	sbut.uButType   = SBBT_TOGGLEBUTTON | SBBM_TYPE2 | SBBM_RECESSED;
	CoolSB_InsertButton(hwnd, SB_VERT, (UINT)-1, &sbut);
	
	sbut.fMask		= SBBF_ID | SBBF_TYPE | SBBF_SIZE | SBBF_PLACEMENT | SBBF_CURSOR;
	sbut.uCmdId		= VERT_GRIPPER;
	sbut.nSize		= 7;
	sbut.uPlacement = SBBP_LEFT;
	sbut.uButType   = SBBT_FIXED;
	sbut.hCurs		= LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEUD));
	CoolSB_InsertButton(hwnd, SB_VERT, (UINT)-1, &sbut);
	
	InitMetaFile();		
	
	CoolSB_ModifyButton(hwnd, SB_HORZ, (UINT)-1,0, &sbut);
}

void RemoveButtons(HWND hwnd)
{
	CoolSB_RemoveButton(hwnd, SB_HORZ, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_HORZ, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_HORZ, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_HORZ, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_HORZ, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_VERT, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_VERT, 0, FALSE);
	CoolSB_RemoveButton(hwnd, SB_VERT, 0, FALSE);
}

//
//	Window procedure for our "standard" window. It doesn't do much
//	apart from draw some text
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT winrect;
	RECT rect;
	NMHDR *hdr;
	static int counter=0;
	static int hscrollpos = 0;
	SCROLLINFO si;
	static int count;
	char *line;
	int i;
	int oldv;
	
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_POS;
	si.nPos = 0;

	switch(message)
	{
	case WM_MOUSEACTIVATE:
		if(GetFocus() != hwnd)
			SetFocus(hwnd);

		return MA_ACTIVATE;

	case WM_SETFOCUS:
		InvalidateRect(hwnd, 0, 0);
		return 0;

	case WM_KILLFOCUS:
		InvalidateRect(hwnd, 0, 0);
		return 0;

	case WM_SIZE:
		oldv = nVScrollPos;
		SetupScrollbars	(hwnd);

		if(nVScrollMax < nPageMaxLines && oldv != nVScrollPos ||
			nVScrollPos != 0 && nVScrollPos == nVScrollMax - nPageMaxLines+1 && oldv != nVScrollPos)
		{
			InvalidateRect(hwndScroll1, NULL, FALSE);
			UpdateWindow(hwndScroll1);
			InvalidateRect(hwndScroll2, NULL, FALSE);
			UpdateWindow(hwndScroll2);

		}

		return 0;

	case WM_HSCROLL:
		if(hwnd == hwndScroll2 || hwnd==hwndScroll1)
		{
			switch(LOWORD(wParam))
			{
			case SB_TOP:	TRACE("Top-Left\n");break;
			case SB_BOTTOM: TRACE("Bottom-Right\n"); break;

			case SB_LINELEFT:
				TRACE("Line Left\n");
				nHScrollPos--;
				if(nHScrollPos < 0) nHScrollPos = 0;
				break;
				
			case SB_LINERIGHT:
				TRACE("Line Right\n");
				nHScrollPos++;
				if(nHScrollPos > nHPageMax) nHScrollPos = nHPageMax;
				break;
				
			case SB_PAGELEFT:
				TRACE("Page Left\n");
				nHScrollPos-=nHScrollPageSize;
				if(nHScrollPos < 0) nHScrollPos = 0;
				break;
				
			case SB_PAGERIGHT:
				TRACE("Page Right\n");
				nHScrollPos+=nHScrollPageSize;
				if(nHScrollPos > nHPageMax) nHScrollPos = nHPageMax;
				break;

			case SB_THUMBTRACK:
				si.nPos = HIWORD(wParam);
				TRACE("ThumbTrack %d\n", si.nPos);
				si.fMask = SIF_TRACKPOS;
				CoolSB_GetScrollInfo(hwnd, SB_HORZ, &si);
				nHScrollPos = si.nTrackPos;
				break;

			case SB_THUMBPOSITION:
				TRACE("ThumbPos %d\n", HIWORD(wParam));
				break;

			case SB_ENDSCROLL:
				TRACE("EndScroll\n");
				break;

			}
			
			CoolSB_SetScrollPos(hwndScroll1, SB_HORZ, nHScrollPos, TRUE);
			CoolSB_SetScrollPos(hwndScroll2, SB_HORZ, nHScrollPos, TRUE);

			InvalidateRect(hwndScroll1, 0,0);
			InvalidateRect(hwndScroll2, 0,0);
		}

		return 0;

	case WM_VSCROLL:
		if(hwnd == hwndScroll2 || hwnd==hwndScroll1)
		{
			switch(LOWORD(wParam))
			{
			case SB_LINEUP:
				TRACE("Line Up\n");
				nVScrollPos--;
				if(nVScrollPos < 0) nVScrollPos = 0;
				break;
				
			case SB_LINEDOWN:
				TRACE("Line Down\n");
				nVScrollPos++;
				if(nVScrollPos > nVScrollMax) nVScrollPos = nVScrollMax;
				break;
				
			case SB_PAGEUP:
				TRACE("Page Up\n");
				nVScrollPos-=nVScrollPageSize;
				if(nVScrollPos < 0) nVScrollPos = 0;
				break;
				
			case SB_PAGEDOWN:
				TRACE("Page Down\n");
				nVScrollPos+=nVScrollPageSize;
				if(nVScrollPos > nVScrollMax) nVScrollPos = nVScrollMax;
				break;

			case SB_THUMBTRACK:
				si.nPos = HIWORD(wParam);
				TRACE("ThumbTrack %d\n", si.nPos);
				si.fMask = SIF_TRACKPOS;
				CoolSB_GetScrollInfo(hwnd, SB_VERT, &si);
				nVScrollPos = si.nTrackPos;
				break;

			case SB_THUMBPOSITION:
				TRACE("ThumbPos %d\n", HIWORD(wParam));
				break;

			case SB_ENDSCROLL:
				TRACE("EndScroll\n");
				break;

			}
			
			CoolSB_SetScrollPos(hwndScroll1, SB_VERT, nVScrollPos, TRUE);

			nVScrollPos = CoolSB_GetScrollPos(hwndScroll1, SB_VERT);

			CoolSB_SetScrollPos(hwndScroll2, SB_VERT, nVScrollPos, TRUE);


			InvalidateRect(hwndScroll1, 0,0);
			InvalidateRect(hwndScroll2, 0,0);
		}

		return 0;


	case WM_ERASEBKGND:
		return 1;
	
	case WM_PAINT:
		GetClientRect(hwnd, &winrect);

		//find the right line
		line = BigFile;

		//search for the start of the current line in the current
		//text file
		for(i = 0; i < nVScrollPos && i < nNumLines; i++)
		{
			line = (char*)memchr(line, '\r', BigFile+BigFileLen - line) + 2;
			if(line == (char *)2)
			{
				break;
			}
		}

		//if we didn't find it, then quit out now
		if(line == (char *)2)
		{
			return DefWindowProc(hwnd, message, wParam, lParam);
		}

		hdc = BeginPaint(hwnd, &ps);

		SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT));

		if(GetFocus() == hwnd)
			SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
		else
			SetTextColor(hdc, GetSysColor(COLOR_BTNSHADOW));

		for(i = 0; i < min(nNumLines, ps.rcPaint.bottom/yChar) + 1; i++)
		{
			int temp;
			int linelen = 0;
			int linechars;
			char *next = (char*)memchr(line, '\r', BigFile+BigFileLen - line) + 1;
			char buffer[256];
			
			if(next == (char *)1)
				break;

			linechars = min(255, next-line-1);
			lstrcpyn(buffer, line, linechars);

			temp = 4;
			linelen = TabbedTextOut(hdc, -nHScrollPos*2, i*13, line, linechars, 0, 0, -nHScrollPos*2);
			linelen = LOWORD(linelen);
			
			//clear the space from the end of the text to the end of the window
			SetRect(&rect, -nHScrollPos*2 + linelen, i*13, winrect.right, (i+1)*13);
			ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "", 0, 0);
			line = next+1;
		}

		//clear any area at the bottom of the window
		SetRect(&rect, 0, i*13, winrect.right, winrect.bottom);
		ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, "", 0, 0);

		//show the focus, if we have it.
		if(GetFocus() == hwnd)
		{
		//	GetClientRect(hwnd, &rect);
		//	DrawFocusRect(hdc, &rect);
		}
		
		EndPaint(hwnd, &ps);
		return 0;

	case WM_COMMAND:

		if(HIWORD(wParam) == CSBN_CLICKED)
		{
			SCROLLBUT sb;
			TRACE("Button clicked: %d\n", LOWORD(wParam));
			sb.uCmdId = LOWORD(wParam);
		}

		return 0;

	//	Mouse messages to handle the splitter-window bar thingy.
	//	the mouse-down message is handled by the WM_NOTIFY handler
	//	when we get a NM_CLICK. However, we set the mouse-capture
	//	at that point, so we will get regular mouse messages to handle
	//	the rest of the splitter logic.
	case WM_MOUSEMOVE:
		Splitter_OnMouseMove(hwnd, message, wParam, lParam);
		return 0;

	case WM_LBUTTONUP:
		Splitter_OnLButtonUp(hwnd, message, wParam, lParam);
		return 0;

	//
	case WM_SETCURSOR:
		if(LOWORD(lParam) == HTCLIENT)
		{
			SetCursor(LoadCursor(0, MAKEINTRESOURCE(IDC_IBEAM)));
				return 0;
		}
		else
		{
			break;
		}


	case WM_NOTIFY:	
		{
			NMTTDISPINFO *ttdi;
			hdr = (NMHDR *)lParam;
			ttdi = (NMTTDISPINFO *)lParam;
			
			if(ttdi->hdr.code == TTN_GETDISPINFO)
			{
				int i;
				TRACE("tooltip text request [%d]\n", ttdi->hdr.idFrom);

				for(i = 0; i < sizeof(TipLookup) / sizeof(TipLookup[0]); i++)
				{
					if(TipLookup[i].cmd == ttdi->hdr.idFrom)
					{
						lstrcpy(ttdi->lpszText, TipLookup[i].text);//"James";
						ttdi->hinst = hInst;
					}
				}
				return 0;
			}
			else if(ttdi->hdr.code == TTN_SHOW)
			{
				TRACE("tooltip showing\n");
			}
			else if(ttdi->hdr.code == TTN_POP)
			{
				TRACE("tooltip hiding\n");
			}
			else if(hdr->code == NM_CUSTOMDRAW)
			{
				return HandleCustomDraw(wParam, (NMCSBCUSTOMDRAW *)lParam);
			}
			else if(hdr->code == NM_CLICK)
			{
				NMCOOLBUTMSG *nmc = (NMCOOLBUTMSG *)lParam;
				POINT pt;
				pt = nmc->pt;

				if(nmc->uCmdId == VERT_GRIPPER)
					Splitter_OnLButtonDown(hwnd, WM_LBUTTONDOWN, wParam, MAKELPARAM(nmc->pt.x, nmc->pt.y));
			}
		}
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}


void InitCoolScrollTest(HINSTANCE hInstance)
{
	//Window class for a typical custom window
	WNDCLASSEX wndclass;

	wndclass.cbSize			= sizeof(wndclass);
	wndclass.style			= 0;//CS_VREDRAW | CS_HREDRAW;
	wndclass.lpfnWndProc	= WndProc;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= 0;
	wndclass.hCursor		= LoadCursor (NULL, IDC_IBEAM);
	wndclass.hbrBackground	= (HBRUSH)0;//GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName	= 0;
	wndclass.lpszClassName	= "James";
	wndclass.hIconSm		= LoadIcon (NULL, IDI_APPLICATION);

	RegisterClassEx(&wndclass);
}

//
//	Display an open-dialog box, and load the selected text file
//	into a block of memory. 
//
void DoOpenFile(HWND hwnd)
{
	HANDLE fp;
	char szFileName[_MAX_PATH] = "";
	char szTitleName[_MAX_FNAME+_MAX_EXT] = "";

	OPENFILENAME ofn;
	static char szFilter[] = "Text Files (*.txt)\0*.txt\0"	\
							 "All Files (*.*)\0*.*\0\0";

	ZeroMemory(&ofn, sizeof ofn);

	ofn.lStructSize = sizeof ofn;
	ofn.Flags = OFN_HIDEREADONLY | OFN_EXPLORER;
	ofn.hInstance = hInst;
	ofn.lpstrFilter = szFilter;
	ofn.nMaxFile = _MAX_PATH;
	ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT;
	ofn.lpstrFile = szFileName;
	ofn.lpstrFileTitle = szTitleName;

	if(GetOpenFileName(&ofn))
	{
		DWORD temp;
		char *line;

		fp = CreateFile(ofn.lpstrFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
		if(fp == INVALID_HANDLE_VALUE)
			return;

		BigFileLen = GetFileSize(fp, 0);

		if(BigFile) 
			HeapFree(GetProcessHeap(), 0, BigFile);

		BigFile = HeapAlloc(GetProcessHeap(), 0, BigFileLen);
		ReadFile(fp, BigFile, BigFileLen, &temp, 0);

		CloseHandle(fp);

		//count the number of lines
		line = BigFile;

		nNumLines = 0;
		while(line != (char *)2)
		{
			line = (char*)memchr(line, '\r', BigFile+BigFileLen - line) + 2;
			if(line != (char *)2) nNumLines++;
		}

		nVScrollPos = 0;
		SetupScrollbars(hwndScroll1);
		SetupScrollbars(hwndScroll2);

		InvalidateRect(hwndScroll1, 0, TRUE);
		InvalidateRect(hwndScroll2, 0, TRUE);

	}

}

void MenuCheckMark (HMENU hmenu, int id, BOOL bCheck)
{
	int iState;
	iState = (bCheck) ? MF_CHECKED : MF_UNCHECKED;
	CheckMenuItem(hmenu, id, iState);
}

BOOL CALLBACK DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	SCROLLINFO si;
	HDWP hdwp;
	int height;
	RECT *prect;
	static HICON hicon;

	switch(uMsg)
	{
	case WM_INITDIALOG:

		hmenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU1));
		SetMenu(hwnd, hmenu);

		hdcSkin  = CreateCompatibleDC(0);
		hSkinBmp = (HBITMAP)LoadImage(0, "skin01.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
		SelectObject(hdcSkin, hSkinBmp);

		hicon = LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0);
		SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);

		hwndScroll1 = CreateWindowEx(WS_EX_CLIENTEDGE, "James", "", 
			WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL,
			2, 2, 200, 200, hwnd, 0, hInst, 0);

		hwndScroll2 = CreateWindowEx(WS_EX_CLIENTEDGE, "James", "", 
			WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL,
			2, 2, 200, 200, hwnd, 0, hInst, 0);

		SetWindowPos(hwnd, 0, 0,0,400,300,SWP_NOMOVE|SWP_NOZORDER);

		si.cbSize = sizeof(si);
		si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
		si.nMin = 0;
		si.nMax = nHPageMax;
		si.nPage = nHScrollPageSize;
		si.nPos = nHScrollPos;
		si.nTrackPos = 0;

		SetWindowLong(hwndScroll1, GWL_USERDATA, 0);		
		

		SetScrollInfo(hwndScroll1, SB_HORZ, &si, TRUE);
		SetScrollInfo(hwndScroll2, SB_HORZ, &si, TRUE);
		
		InitializeCoolSB(hwndScroll2);

		SetupScrollbars(hwndScroll1);
		SetupScrollbars(hwndScroll2);

		InsertButtons(hwndScroll2);		

		CoolSB_SetStyle(hwndScroll1, SB_BOTH, CSBS_NORMAL);
		CoolSB_SetStyle(hwndScroll2, SB_BOTH, CSBS_NORMAL);

		return 0;

	case WM_SIZE:
		hdwp = BeginDeferWindowPos(2);

		height = HIWORD(lParam);
		DeferWindowPos(hdwp, hwndScroll1, 0, 0, 0, LOWORD(lParam), height/2, SWP_DRAWFRAME|SWP_FRAMECHANGED);
		DeferWindowPos(hdwp, hwndScroll2, 0, 0, height-(height/2), LOWORD(lParam), height/2, SWP_DRAWFRAME|SWP_FRAMECHANGED);

		EndDeferWindowPos(hdwp);
		//CoolSB_EnableScrollBar(hwndScroll2, SB_VERT, ESB_DISABLE_RIGHT);
		return 0;

	case WM_SIZING:
		prect = (RECT *)lParam;

		prect->bottom &= ~1;
		prect->top    &= ~1;
		return TRUE;

	case WM_CLOSE:
		DeleteEnhMetaFile(hemf);
		EndDialog(hwnd, 0);
		return 0;
	
	//when get this message, rebuild our EMF picture so it fits properly
	//in the scrollbars

		return 0;


	case WM_COMMAND:
		if(LOWORD(wParam) == VK_ESCAPE)
		{
			EndDialog(hwnd, 0);
			return 0;
		}

		switch(LOWORD (wParam))
		{
		case IDM_FILE_OPEN:
			DoOpenFile(hwnd);
			return 0;

		case ID_VIEW_NORMAL:
			CoolSB_SetStyle(hwndScroll2, SB_BOTH, CSBS_NORMAL);
			MenuCheckMark(hmenu, ID_VIEW_NORMAL, TRUE);
			MenuCheckMark(hmenu, ID_VIEW_FLAT, FALSE);
			MenuCheckMark(hmenu, ID_VIEW_HOTTRACKED, FALSE);
			return 0;

		case ID_VIEW_FLAT:
			CoolSB_SetStyle(hwndScroll2, SB_BOTH, CSBS_FLAT);
			MenuCheckMark(hmenu, ID_VIEW_NORMAL, FALSE);
			MenuCheckMark(hmenu, ID_VIEW_FLAT, TRUE);
			MenuCheckMark(hmenu, ID_VIEW_HOTTRACKED, FALSE);

			return 0;

		case ID_VIEW_HOTTRACKED:
			CoolSB_SetStyle(hwndScroll2, SB_BOTH, CSBS_HOTTRACKED);
			MenuCheckMark(hmenu, ID_VIEW_NORMAL, FALSE);
			MenuCheckMark(hmenu, ID_VIEW_FLAT, FALSE);
			MenuCheckMark(hmenu, ID_VIEW_HOTTRACKED, TRUE);
			return 0;

		case ID_VIEW_CUSTOMDRAW:
			fCustomDraw = !fCustomDraw;
			ToggleMenuCheck(hmenu, ID_VIEW_CUSTOMDRAW);

			if(fCustomDraw)
			{
				CoolSB_SetSize(hwndScroll2, SB_BOTH, 18, 18);
				CoolSB_SetMinThumbSize(hwndScroll2, SB_BOTH, 18);
			}
			else
			{
				CoolSB_SetSize(hwndScroll2, SB_BOTH, SYSTEM_METRIC, SYSTEM_METRIC);
				CoolSB_SetMinThumbSize(hwndScroll2, SB_BOTH, (UINT)-1);
			}
			
			RedrawNC(hwndScroll2);
			return 0;

		case ID_VIEW_INSERTEDBUTTONS:
			fButtons = !fButtons;
			ToggleMenuCheck(hmenu, ID_VIEW_INSERTEDBUTTONS);

			if(fButtons)
			{
				InsertButtons(hwndScroll2);
			}
			else
			{
				RemoveButtons(hwndScroll2);
			}
			RedrawNC(hwndScroll2);
			return 0;

		case ID_VIEW_THUMBALWAYS:
			fThumbAlways = !fThumbAlways;
			ToggleMenuCheck(hmenu, ID_VIEW_THUMBALWAYS);

			CoolSB_SetThumbAlways(hwndScroll2, SB_VERT, fThumbAlways);
			RedrawNC(hwndScroll2);
			return 0;

		case ID_FILE_ABOUT:
			MessageBox(hwnd, "Cool Scrollbar library\n\rBy J Brown", "CoolScroll Demo", MB_OK | MB_ICONINFORMATION);
			return 0;

		case IDCANCEL: 
			EndDialog(hwnd, 0);
			break;
		}
		break;


	}
	

	return 0;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	hInst = hInstance;

	InitCoolScrollTest(hInstance);

	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, DlgProc);

	UNREFERENCED_PARAMETER(szCmdLine);
	UNREFERENCED_PARAMETER(iCmdShow);
	UNREFERENCED_PARAMETER(hPrevInstance);
	return 0;
}
